package com.sugar.queryinfo.module.business.controller.admin.infocomparison;

import com.sugar.queryinfo.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.queryinfo.module.business.util.HideUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.controller.admin.infocomparison.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.infocomparison.InfoComparisonDO;
import com.sugar.queryinfo.module.business.service.infocomparison.InfoComparisonService;

@Tag(name = "管理后台 - 智能比对")
@RestController
@RequestMapping("/business/info-comparison")
@Validated
public class InfoComparisonController {

    @Resource
    private InfoComparisonService infoComparisonService;

    @PostMapping("/create")
    @Operation(summary = "创建智能比对")
    @PreAuthorize("@ss.hasPermission('business:info-comparison:create')")
    public CommonResult<List<InfoComparisonSaveRespVO>> createInfoComparison(@Valid @RequestBody InfoComparisonSaveReqVO createReqVO) {
        return success(infoComparisonService.createInfoComparison(createReqVO.getContent(),
                SecurityFrameworkUtils.getLoginUserId(), SecurityFrameworkUtils.getLoginUserDeptId()));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除智能比对")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:info-comparison:delete')")
    public CommonResult<Boolean> deleteInfoComparison(@RequestParam("id") Long id) {
        infoComparisonService.deleteInfoComparison(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除智能比对")
                @PreAuthorize("@ss.hasPermission('business:info-comparison:delete')")
    public CommonResult<Boolean> deleteInfoComparisonList(@RequestParam("ids") List<Long> ids) {
        infoComparisonService.deleteInfoComparisonListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得智能比对")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:info-comparison:query')")
    public CommonResult<InfoComparisonRespVO> getInfoComparison(@RequestParam("id") Long id) {
        InfoComparisonDO infoComparison = infoComparisonService.getInfoComparison(id);
        return success(BeanUtils.toBean(infoComparison, InfoComparisonRespVO.class, respVO ->
                respVO.setName(HideUtil.name(respVO.getName()))
                        .setMobile(HideUtil.mobile(respVO.getMobile()))));
    }

    @GetMapping("/page")
    @Operation(summary = "获得智能比对分页")
    @PreAuthorize("@ss.hasPermission('business:info-comparison:query')")
    public CommonResult<PageResult<InfoComparisonRespVO>> getInfoComparisonPage(@Valid InfoComparisonPageReqVO pageReqVO) {
        PageResult<InfoComparisonDO> pageResult = infoComparisonService.getInfoComparisonPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InfoComparisonRespVO.class/*, respVO ->
                respVO.setName(HideUtil.name(respVO.getName()))
                        .setMobile(HideUtil.mobile(respVO.getMobile()))*/));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出智能比对 Excel")
    @PreAuthorize("@ss.hasPermission('business:info-comparison:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInfoComparisonExcel(@Valid InfoComparisonPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InfoComparisonDO> list = infoComparisonService.getInfoComparisonPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "智能比对.xls", "数据", InfoComparisonRespVO.class,
                        BeanUtils.toBean(list, InfoComparisonRespVO.class));
    }

}